﻿@model IPagination<Agreement>
@using BankCreditPortfolio.DAL.Entities
@using MvcContrib.Pagination
@using MvcContrib.UI.Grid
@using BankCreditPortfolio.DAL.Services
@using MvcContrib.UI.Pager

@{
    ViewBag.Title = "Credits";
}
<strong>Credits</strong>
<p>Here you can see the list of all active credits.</p>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/Styles/forms.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/Styles/customgrid.css")" rel="stylesheet" type="text/css" />

@using (Html.BeginForm())
{
    <div>
@{
    var payments = new PaymentService();
	if (Model.Count() != 0)
	{
		var grid = Html.Grid(Model).Columns(column =>
			 {
                 column.For(x => x.Order.Client.Profile.FirstName + " " + x.Order.Client.Profile.LastName).Named("Client");
                 column.For(x => x.Credit.DeliveryDate.ToShortDateString()).Named("Delivery Date"); 
				 column.For(x => x.Order.Amount).Named("Amount");
                 column.For(x => x.Order.Currency.Title).Named("Currency");
                 column.For(x => x.Order.Tariff.CreditPercent).Named("Percent");
                 column.For(x => payments.GetAllByDepositId(x.Credit.Deposit.DepositId) != null ?
                     payments.GetAllByDepositId(x.Credit.Deposit.DepositId).ToList().Sum(payment => payment.Amount) : 0)
                     .Named("Discharged");
                 column.For(x => payments.GetAllByDepositId(x.Credit.Deposit.DepositId) != null ?
                      x.Order.Amount * (1 + x.Order.Tariff.CreditPercent * Convert.ToDecimal(0.01)) -
                      payments.GetAllByDepositId(x.Credit.Deposit.DepositId).ToList().Sum(payment => payment.Amount) : 
                      x.Order.Amount * (1 + x.Order.Tariff.CreditPercent * Convert.ToDecimal(0.01)))
                     .Named("Reminder");
                 column.For(x => Html.ActionLink(x.Order.Tariff.Title, "Tariff", "Home", new { tariffId = x.Order.Tariff.TariffId }, null)).Named("Tariff");
                 column.For(x => x.Order.Guarantors.Count > 0 ? x.Order.Guarantors.First().Profile.FirstName + " " +
                x.Order.Guarantors.First().Profile.MiddleName + " " + x.Order.Guarantors.First().Profile.LastName : string.Empty).Named("Guarantor");
                 column.For(x => Html.ActionLink("Agreement", "ShowAgreement", "Bank", new { agreementId = x.AgreementId }, new { target = "_blank" })).Named("Agreement");
			 });
		grid.Render();
        <br />
        @Html.Pager(Model).First("First").Last("Last").Next("Next").Previous("Previous")
    }
    else
    {
		<div>There is no credits.</div>
    }
}
    </div> 
}